package com.wlc.base.lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王立朝 wanglichao@champion-credit.com
 * @description Lambda 聚合操作
 * @date 2021-07-14 10:54
 */
public class LambdaStream {
    public void method01() {
        List<Hero> heroes = TestLambda.initData();
        heroes.forEach(h -> System.out.println(h.getName() + " " + h.getHp() + " " + h.getDamage()));
        heroes.stream()
                .filter(h -> h.getHp() > 100 && h.getDamage() < 50)
                .forEach(h -> System.out.println("筛选后的结果为：" + h.getName()));
    }

    public void method02() {
        List<Hero> heroes = new ArrayList<>();
        heroes.add(new Hero("zhangsan", 23, 34));
        heroes.add(new Hero("zhangsan", 12, 44));
        heroes.add(new Hero("lisi", 23, 4));
        heroes.add(new Hero("lisi", 12, 44));
        heroes.stream()
                .map((h) -> h.getName() + "-" + h.getHp() + "-" + h.getDamage()).forEach(h -> System.out.println(h));
        heroes
                .stream()
                .map((h) -> h.getName() + " - " + h.getHp() + " - " + h.getDamage())
                .forEach(h -> System.out.println(h));
        // 筛选过后重新放入到新的集合中国
        List<Hero> collect = heroes.stream().filter(h -> h.getHp() < 20).collect(Collectors.toList());
        collect.forEach(System.out::print);
    }

    public static void main(String[] args) {
        new LambdaStream().method02();
    }
}
